package o2sjsc.projects.erp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class SplashActivity extends Activity {
	private static String url = "http://dl.dropbox.com/spa/khn1gncb4rznksv/WVersionManager/public/version_2.txt";
	private static final String TAG_VERSION = "version_code";
	private CheckVersionTask mCheckVersionTask = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes
																// notification
																// bar

		setContentView(R.layout.activity_splash);

		mCheckVersionTask = new CheckVersionTask();
		mCheckVersionTask.execute((Void) null);
		mCheckVersionTask.doInBackground((Void) null);
		// CheckVersionTask(SplashActivity.this).execute();
	}

	private class CheckVersionTask extends AsyncTask<Void, Void, Boolean> {
		Integer mResult;
		final Context context = SplashActivity.this;

		protected Boolean doInBackground(Void... params) {

			// JSONParser jParser = new JSONParser();
			// getting JSON string from URL
			// JSONArray json = jParser.getJSONFromUrl(url);

			try {

				/*
				 * JSONObject c = json.getJSONObject(0); String vtype =
				 * c.getString(TAG_VERSION); int versionAvailable
				 * =Integer.parseInt(vtype);
				 */

				int versionAvailable = 1;
				PackageInfo manager = getPackageManager().getPackageInfo(
						getPackageName(), 0);
				int versionCurrent = manager.versionCode;
				if (versionCurrent < versionAvailable) {
					mResult = versionAvailable;
				} else {
					mResult = 0;
				}
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}

			return true;
		}

		@Override
		protected void onPostExecute(final Boolean success) {
			mCheckVersionTask = null;
			if (success) {
				if (mResult == 0) {
					// run login activity
					finish();
					runLoginActivity();
				} else {
					AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
							context);

					// set title
					alertDialogBuilder.setTitle("Update Available");

					// set dialog message
					alertDialogBuilder
							.setMessage(
									"Version "
											+ mResult
											+ " is available! Do you want to update it?")
							.setCancelable(false)
							.setPositiveButton("Yes",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											// if this button is clicked, update
											// the application
											finish();
										}
									})
							.setNegativeButton("No",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											// if this button is clicked, just
											// close
											// the dialog box and the
											// application continues
											dialog.cancel();
											finish();
											runLoginActivity();
										}
									});

					// create alert dialog
					AlertDialog alertDialog = alertDialogBuilder.create();
					// show it
					alertDialog.show();
				}
			}
		}

		private void runLoginActivity() {
			Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
			startActivity(intent);
		}

		@Override
		protected void onCancelled() {
			mCheckVersionTask = null;

		}
	}
}